// import storage from '../utils/MySessionStorage'
import localStorage from '../../utils/MyLocalStorage'
// import { login, logout, getInfo } from '@/api/login'
import api from '../../api';
import axios from "../../utils/request";
// 将json字符串转换为json对象
// JSON.parse(字符串) //将该字符串转为json对象给前端使用
// 将json对象转换为json字符串
// JSON.stringify(json对象) //将json对象转换为json字符串，传给后端
const state = {
  user: localStorage.get('user')|| {},
  token:localStorage.get('token')||'',
}

const getters = {
  token: token => state.token,
}

//方法参考地址
//https://www.jb51.net/article/261401.htm
const mutations = {
  //保存用户数据
  saveUser(state,user){
    state.user = user;
  },
  //保存token信息
  setToken: (state, token) => {
    state.token = token
  },

}
const actions = {
  /**
   * 用户登录
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  acLogin: function ({commit}, params) {

    return new Promise((resolve, reject) => {

      axios.post('/api/login', params).then((res) => {
        // console.log(JSON.stringify(res));
          const user = res.data.user
          const token = res.data.token
          user.password = ''//清空密码
          localStorage.set("user",user,60*60*4)//4个小时
          localStorage.set(api.tokenKey, token, 60*60*4)//4个小时
          commit("saveUser", user)//保存用户数据
          commit('setToken', token)//保存token
          resolve()
      }).catch((error) => {
        reject(error)
      })

    })

  },

  /**
   * 用户注册
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  acRegister: function ({commit}, params) {

    return new Promise((resolve, reject) => {
      axios.post('/api/register', params).then((res) => {
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    })

  },

  /**
   * 注销
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  logout: function ({commit}) {
    //注销用户信息，从token中获取信息，不需要传入其他信息
    return new Promise((resolve, reject) => {
      axios.get('/api/logout').then((res) => {
        //清空token信息
        localStorage.remove('token')
        //清空用户信息
        localStorage.remove('user')
        //如果是学生用户，需要清空学生信息
        if(state.user.userType==1){
          localStorage.remove('student_user')
        }
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    })

  },


  /**
   * 修改密码
   * @param commit
   * @param params
   * @returns {Promise<unknown>}
   */
  updatePassword: function ({commit}, form) {
    // console.log(JSON.stringify(form));
    return new Promise((resolve, reject) => {
      axios.post('/api/updatePassword', form).then((res) => {
        //清空token信息
        localStorage.remove('token')
        //清空用户信息
        localStorage.remove('user')
        //如果是学生用户，需要清空学生信息
        if(state.user.userType==1){
          localStorage.remove('student_user')
        }
        resolve(res)
      }).catch((error) => {
        reject(error)
      })

    })

  },

}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
